package com.hxzy.tzxm.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxzy.common.core.domain.entity.SysRole;
import com.hxzy.common.utils.bean.BeanUtils;
import com.hxzy.tzxm.domain.CmsCategory;
import com.hxzy.tzxm.domain.CmsContent;
import com.hxzy.tzxm.domain.CmsContentAttach;
import com.hxzy.tzxm.domain.dto.CmsContentSearchDTO;
import com.hxzy.tzxm.domain.vo.CmsContentVO;
import com.hxzy.tzxm.mapper.CmsContentMapper;
import com.hxzy.tzxm.service.CmsContentAttachService;
import com.hxzy.tzxm.service.CmsContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 文章列表
 */
@Service
public class CmsContentServiceImpl extends ServiceImpl<CmsContentMapper, CmsContent> implements CmsContentService {


    @Autowired
   private CmsContentAttachService cmsContentAttachService;

    @Override
    public IPage<CmsContent> search(CmsContentSearchDTO cmsContentSearchDTO) {
        IPage<CmsContent> page= PageDTO.of(cmsContentSearchDTO.getPageNum(),cmsContentSearchDTO.getPageSize());
        return  this.baseMapper.search(page,cmsContentSearchDTO);
    }

    @Override
    public CmsContentVO findById(Long id) {
        return this.baseMapper.findById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean add(CmsContentVO cmsContentVO) {

        CmsContent  cmsContent=new CmsContent();
        BeanUtils.copyProperties(cmsContentVO,cmsContent);

        boolean save = this.save(cmsContent);
        if(save){
            // 保存附件
            List<CmsContentAttach> attachList = cmsContentVO.getAttachList();
            if(attachList!=null && attachList.size()>0){
                for (CmsContentAttach item:  attachList) {
                    item.setContentId( cmsContent.getId());
                }

                //批量新增
                save= this.cmsContentAttachService.saveBatch(attachList);
            }
        }
        return save;
    }

    @Override
    public boolean edit(CmsContentVO cmsContentVO) {
        CmsContent  cmsContent=new CmsContent();
        BeanUtils.copyProperties(cmsContentVO,cmsContent);

        boolean save = this.updateById(cmsContent);

        if(save){
            //先删除掉原先的附件信息
            this.cmsContentAttachService.deleteByContentId(cmsContent.getId());

            // 再新增保存附件
            List<CmsContentAttach> attachList = cmsContentVO.getAttachList();
            if(attachList!=null && attachList.size()>0){
                for (CmsContentAttach item:  attachList) {
                    item.setContentId( cmsContent.getId());
                }
                //批量新增
                save= this.cmsContentAttachService.saveBatch(attachList);
            }
        }

        return save;
    }

    @Override
    public boolean deleteById(long id) {
        return this.baseMapper.updateDelFlag(id)>0;
    }
}
